package Agency.models;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.swing.JOptionPane;
import Agency.views.*;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author wangari
 */
public class EditTenantInformation extends javax.swing.JFrame {
    Connection conn = null;
    private ActionListener actionlistener;
    public EditTenantInformationView myTenantEditor;
    
    /**
     * Constructor  that instantiates  a new object of the  edit view
     */
    public EditTenantInformation (){
      myTenantEditor= new EditTenantInformationView();
      //myTenantEditor.setVisible(true);
    }
    /**
     * method to close the view, it first clears all the fields and later closes the view
     * 
     */
    public void closeTheView(){
        clearTextFields();
    myTenantEditor.setVisible(false);    
    }
    /**
     * Searching the tenant, you take the id number and display t
     * he information of the tenant who corresponds to that number so that you can edit.
     * The id number of the tenant is not editable
     */
   public void searchTheTenant(){
    
       String    theTenantsIdNo = JOptionPane.showInputDialog ( "Enter the Id number of the Tenant you want to delete");
         try {
             
       Class.forName("com.mysql.jdbc.Driver").newInstance();
 conn = java.sql.DriverManager.getConnection("jdbc:mysql://localhost/MusyiAgency?user=root&password=");
 System.out.println("connected to database");
        PreparedStatement p =conn.prepareStatement("SELECT * FROM TenantsInformation WHERE TenantsIdNo = "+theTenantsIdNo+";");


  java.sql.ResultSet r = p.executeQuery ();
  while(r.next()) {
      myTenantEditor.getTenantName().setText(r.getString("TenantsName"));
      myTenantEditor.getTenantIdNum().setText(r.getString("TenantsIdNo"));
      myTenantEditor.getTenantPhoneNum().setText(r.getString("TenantsPhoneNumber"));
      myTenantEditor.getTenantHseType().setSelectedItem(r.getString("HouseType"));
      myTenantEditor.getPlotNum().setText(r.getString("HousePlotNum"));
      myTenantEditor.getTenantHseId().setText(r.getString("HouseId"));
      myTenantEditor.getTenantTown().setSelectedItem(r.getString("Town"));
      myTenantEditor.getTenantGender().setSelectedItem(r.getString("Gender"));
      myTenantEditor.getTenantOccupation().setText(r.getString("Occupation"));
    myTenantEditor.getTenantIdNum().setEditable(false) ;  
  }
}
       catch (Exception e) {
  JOptionPane.showMessageDialog(null,e);
        }
    }
   /**
    * Clearing all the text fields by setting them to null
    */
   public void clearTextFields(){
     myTenantEditor.getTenantIdNum().setText(null);
     myTenantEditor.getTenantName().setText(null);
     myTenantEditor.getTenantPhoneNum().setText(null);
     myTenantEditor.getPlotNum().setText(null);
     myTenantEditor.getTenantOccupation().setText(null);
   }
   /**
    * Saving the changes after editing the tenants information
    */
   public void saveTheChanges(){
       String   tenantIdNum =myTenantEditor.getTenantIdNum().getText();      
        String tenantName = myTenantEditor.getTenantName().getText();
        String  tenantPhoneNum = myTenantEditor.getTenantPhoneNum().getText();
        String houseType = myTenantEditor.getTenantHseType().getSelectedItem().toString();
        String  housePlotNum =myTenantEditor.getPlotNum().getText();
        String theTown = myTenantEditor.getTenantTown().getSelectedItem().toString();
        String theGender = myTenantEditor.getTenantGender().getSelectedItem().toString();
        String theOccupation =myTenantEditor.getTenantOccupation().getText();
  try{
       Class.forName("com.mysql.jdbc.Driver").newInstance();
 conn = java.sql.DriverManager.getConnection(
 "jdbc:mysql://localhost/MusyiAgency?user=root&password=");
 
    PreparedStatement p=conn.prepareStatement("UPDATE TenantsInformation set TenantsName=?, TenantsIdNo=? ,TenantsPhoneNumber =?,HouseType=?,"
            + "HousePlotNum=?,Town=?,Gender=?,Occupation=? WHERE TenantsIdNo = "+tenantIdNum+";");
    p.setString(1,  tenantName);
    p.setString(2, tenantIdNum);
    p.setString(3,tenantPhoneNum );
    p.setString(4,houseType);
    p.setString(5,housePlotNum);
    p.setString(6,theTown);
    p.setString(7,theGender);
    p.setString(8,theOccupation);
    p.execute(); 
    JOptionPane.showMessageDialog(null,"changes saved successfully");
    clearTextFields();
    p.close();
   
    }catch(Exception e){
        System.out.println("Error"+e.toString());
        //return;        
    }
   }
   
   
   
    
    
    
}
